//
//  Location+Location.m
//  ClinicalCollections
//
//  Created by janakiraman gopinath on 2/10/13.
//  Copyright (c) 2013 GOPI. All rights reserved.
//

#import "Location+Location.h"

@implementation Location (Location)
/*
 + (NSDictionary *) locationLatLong:(NSArray *) items
 forNCTID:(NSString *) nctID
 {
 NSMutableDictionary *returnDictionary = [[NSMutableDictionary alloc] initWithCapacity:MAX_CAPACITY];
 for (int i = 0; i < items.count; i++) {
 if ([nctID isEqualToString:[items[i] valueForKey:@"id"]]) {
 NSArray *cities = [items[i] valueForKey:@"cities"];
 if (cities.count)
 [returnDictionary setObject:[cities objectAtIndex:0] forKey:@"city"];
 
 NSArray *locationLatLong = [items[i] valueForKey:@"location_latlong"];
 
 if (locationLatLong.count) {
 NSString *location_latlong = [locationLatLong objectAtIndex:0];
 
 NSArray *latLong = [location_latlong  componentsSeparatedByString:@","];
 
 if (latLong != nil) {
 [returnDictionary setObject:[[NSNumber alloc] initWithFloat:[latLong[0] floatValue]]forKey:@"lat"];
 [returnDictionary setObject:[[NSNumber alloc] initWithFloat:[latLong[1] floatValue]]forKey:@"lng"];
 }
 }
 
 return returnDictionary;
 }
 }
 return nil;
 }NCT00106951
 */
+ (NSDictionary *) locationLatLong:(NSArray *) items
                          forNCTID:(NSString *) nctID
{
    NSMutableDictionary *returnDictionary = [[NSMutableDictionary alloc] initWithCapacity:MAX_CAPACITY];
    
    if (!([[items valueForKeyPath:@"geodata.formatted"] isEqual:[NSNull null]]) &&
        [[items valueForKeyPath:@"geodata.formatted"] count] > 0)
    {
        [returnDictionary setObject:
         [[items valueForKeyPath:@"geodata.formatted"] objectAtIndex:0] 
                             forKey:@"city"];
    }
    
    if (!([[items valueForKeyPath:@"geodata.latitude"] isEqual:[NSNull null]]) &&
        [[items valueForKeyPath:@"geodata.latitude"] count] > 0)
    {
        [returnDictionary setObject:[[items valueForKeyPath:@"geodata.latitude"] objectAtIndex:0]
                             forKey:@"lat"];
    }
    if (!([[items valueForKeyPath:@"geodata.longitude"]  isEqual:[NSNull null]]) &&
        [[items valueForKeyPath:@"geodata.longitude"] count] > 0)
    {
        [returnDictionary setObject:[[items valueForKeyPath:@"geodata.longitude"] objectAtIndex:0] forKey:@"lng"];
    }
    
    return  returnDictionary;
}
+ (void) locations:(NSArray *) locations
        forContext:(NSManagedObjectContext *)context
forClinicalTrialGov:(ClinicalTrialGov *)clinicalTrialGov
{
    for (int i = 0; i < locations.count; i++) {
        Location  *location = (Location *)[NSEntityDescription insertNewObjectForEntityForName:@"Location"
                                                                        inManagedObjectContext:context];
        
        if (![[locations[i] valueForKeyPath:@"geodata.formatted"] isEqual:[NSNull null]])
            location.city = [locations[i] valueForKeyPath:@"geodata.formatted"];
        else
            location.city = @"";
        
        if (![[locations[i] valueForKeyPath:@"geodata.latitude"] isEqual:[NSNull null]])
            location.lat = [locations[i] valueForKeyPath:@"geodata.latitude"];
        else
            location.lat = [[NSNumber alloc] initWithFloat:0.0];
        
        if (![[locations[i] valueForKeyPath:@"geodata.longitude"] isEqual:[NSNull null]])
            location.lng = [locations[i] valueForKeyPath:@"geodata.longitude"];
        else
            location.lng = [[NSNumber alloc] initWithFloat:0.0];
        
        location.clinicalTrialGov = clinicalTrialGov;
        
        // NSLog(@"Location = %@" , location);
    }
    return;
}

@end
